package com.flow.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.flow.dao.IRolePermissionDao;
import com.flow.model.PagerModel;
import com.flow.model.PermissionMenu;
import com.flow.model.RolePermission;
import com.flow.model.RoleTab;
import com.flow.service.IRolePermissionService;
import com.flow.tools.StringTools;

/**
 * 角色权限服务
 * 
 * @author wang
 * @2016-1-7 RolePermissionService.java
 * @param <T>
 * 
 */
public class RolePermissionService<T> implements IRolePermissionService<T> {

	/**
	 * Dao层引用
	 */
	private IRolePermissionDao<T> rpDao;

	/**
	 * 更新角色权限
	 */
	@Override
	public boolean updateRolePermission(RolePermission rolePermission) {

		if (null == rolePermission)
			return false;

		if (null == rolePermission.getRpId() || 0 == rolePermission.getRpId())
			return false;

		if (null == rolePermission.getPermissionMenu())
			return false;

		if (null == rolePermission.getRoleTab())
			return false;

		if (rpDao.updateRolePermission(rolePermission))
			return true;

		return false;
	}

	/**
	 * 根据角色Id查询角色权限
	 */
	@Override
	public List<T> findRolePermissionByRtId(String rtId) {

		List<T> list = null;

		if (!StringTools.nullOrEmpty(rtId))
			return null;

		try {

			if (null == rtId) {
				String hql = "from RolePermission";
				list = rpDao.findRolePermissionAllByHql(hql);
			} else {
				String hql = "from RolePermission where roleTab.rtId = ?";
				list = rpDao.findRolePermissionAllByHql(hql,
						Integer.valueOf(rtId));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 根据角色ID获取角色列表并分页
	 */
	@Override
	public PagerModel findRolePermissionByPager(String rtId) {

		PagerModel pager = new PagerModel();

		try {

			if (!StringTools.nullOrEmpty(rtId))
				return null;

			String hql = "from RolePermission where roleTab.rtId = ?";
			pager = this.rpDao.findRolePermissionPagerModeled(pager, hql,
					Integer.valueOf(rtId));

		} catch (Exception e) {

			e.printStackTrace();

		}

		return pager;
	}

	/**
	 * 根据ID 查询
	 */
	@Override
	public RolePermission findRolePermissionById(String rpId) {

		if (!StringTools.nullOrEmpty(rpId))
			return null;

		try {
			String hql = "from RolePermission where rpId = ?";
			return (RolePermission) this.rpDao.findRolePermissionByHql(hql,
					Integer.valueOf(rpId));

		} catch (Exception e) {

			e.printStackTrace();

		}

		return null;
	}

	/**
	 * 设置角色增删改查权限
	 */
	@Override
	public boolean setPermissionInfo(String rpId, String rpInsert,
			String rpDelete, String rpUpdate, String rpFind) {

		if (!StringTools.nullOrEmpty(rpId))
			return false;

		if (!StringTools.nullOrEmpty(rpId))
			return false;

		if (!StringTools.nullOrEmpty(rpDelete))
			return false;

		if (!StringTools.nullOrEmpty(rpUpdate))
			return false;

		if (!StringTools.nullOrEmpty(rpFind))
			return false;

		try {
			String hql = "update RolePermission set rpInsert = ?, rpDelete = ? , rpUpdate = ? , rpFind = ? where rpId = ?";
			List<Object> params = new ArrayList<Object>();
			params.add(Integer.valueOf(rpInsert));
			params.add(Integer.valueOf(rpDelete));
			params.add(Integer.valueOf(rpUpdate));
			params.add(Integer.valueOf(rpFind));
			params.add(Integer.valueOf(rpId));
			if (this.rpDao.universalMethod(hql, params) > 0)
				return true;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return false;

	}

	/**
	 * 更新角色权限菜单
	 */
	@Override
	public boolean setRolePermissionInfo(String rtId,
			List<Integer> checkedStatus, String pmId) {

		try {

			// 需要删除的菜单ID集合
			List<Integer> removeList = new ArrayList<Integer>();

			if (null == checkedStatus || !StringTools.nullOrEmpty(rtId)) {
				return false;
			}

			if (StringTools.nullOrEmpty(pmId)) {

				// 将角色原来拥有的菜单ID解析成Integer ID集合
				String[] arr = pmId.split(",");
				List<Integer> idList = new ArrayList<Integer>();
				for (int i = 0; i < arr.length; i++) {
					if (StringTools.nullOrEmpty(arr[i])) {
						idList.add(Integer.valueOf(arr[i]));
					}

				}

				// 获取需要移除的菜单ID
				for (int i = 0; i < idList.size(); i++) {
					if (checkedStatus.indexOf(idList.get(i)) == -1) {
						removeList.add(idList.get(i));
					}
				}

				// 获取需要添加的菜单ID
				for (int i = 0; i < checkedStatus.size();) {
					if (idList.indexOf(checkedStatus.get(i)) != -1) {
						checkedStatus.remove(i);
						i = 0;
					} else {
						i++;
					}
				}

				// 将需要移除的菜单ID删除
				if (null != removeList) {
					for (Integer integer : removeList) {
						String hql = "delete RolePermission where permissionMenu.pmId = ? and roleTab.rtId = ?";
						List<Object> params = new ArrayList<Object>();
						params.add(integer);
						params.add(Integer.valueOf(rtId));
						if (0 >= rpDao.universalMethodRolePermission(hql,
								params))
							return false;
					}
				}
			}

			// 将添加菜单
			if (null != checkedStatus) {
				for (Integer integer : checkedStatus) {
					RoleTab rt = new RoleTab();
					rt.setRtId(Integer.valueOf(rtId));
					PermissionMenu pm = new PermissionMenu();
					pm.setPmId(integer);
					RolePermission rp = new RolePermission(rt, pm);
					rpDao.insertRolePermission(rp);
				}
			}
			return true;

		} catch (Exception e) {

			e.printStackTrace();

		}

		return false;

	}

	/**
	 * 根据多个角色ID查询
	 */
	@Override
	public List<T> findRolePermissionByrtIdList(List<Object> rtIdList) {

		if (null == rtIdList || rtIdList.isEmpty())
			return null;

		try {

			for (Object object : rtIdList) {
				if (null == object)
					rtIdList.remove(object);
			}

			String hql = "from RolePermission where roleTab.rtId in (?) ";
			return rpDao.findRolePermissionAllByHql(hql, rtIdList);

		} catch (Exception e) {

			e.printStackTrace();

		}

		return null;
	}

	public void setRpDao(IRolePermissionDao<T> rpDao) {
		this.rpDao = rpDao;
	}

}
